package com.ruoyi.web.controller.market.account;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.db.domain.ProductInfo;
import com.ruoyi.db.model.param.ChannelInfoParam;
import com.ruoyi.extend.service.IChannelInfoService;
import com.ruoyi.product.service.IProductInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.AccountItemChild;
import com.ruoyi.account.service.IAccountItemChildService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.wapper.AccountItemChildWapper;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 子账户 绑定信息Controller
 * 
 * @author ruoyi
 * @date 2023-02-16
 */
@Controller
@RequestMapping("/account/child")
public class AccountItemChildController extends BaseController
{
    private String prefix = "account/child";

    @Autowired
    private IAccountItemChildService accountItemChildService;
    @Autowired
    private IProductInfoService productInfoService;
    @Autowired
    private IChannelInfoService channelInfoService;

    @RequiresPermissions("account:child:view")
    @GetMapping("/{accountId}/{accountType}")
    public String child(ModelMap modelMap,
                        @PathVariable("accountId") String accountId,
                        @PathVariable("accountType") String accountType)
    {
        modelMap.put("accountId", accountId);
        modelMap.put("accountType", accountType);
        return prefix + "/child";
    }

    /**
     * 查询子账户 绑定信息列表
     */
    @RequiresPermissions("account:child:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AccountItemChild accountItemChild)
    {
        startPage();
        List<Map<String, Object>> mapList = accountItemChildService.selectAccountItemChildListMap(accountItemChild);
        mapList = new AccountItemChildWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 导出子账户 绑定信息列表
     */
    @RequiresPermissions("account:child:export")
    @Log(title = "子账户 绑定信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AccountItemChild accountItemChild)
    {
        List<AccountItemChild> list = accountItemChildService.selectAccountItemChildList(accountItemChild);
        ExcelUtil<AccountItemChild> util = new ExcelUtil<AccountItemChild>(AccountItemChild.class);
        return util.exportExcel(list, "子账户 绑定信息数据");
    }

    /**
     * 新增子账户 绑定信息
     */
    @GetMapping("/add/{accountId}/{accountType}")
    public String add(ModelMap modelMap,
                      @PathVariable("accountId") String accountId,
                      @PathVariable("accountType") String accountType) {
        modelMap.put("accountId", accountId);
        modelMap.put("accountType", accountType);

        if (StrUtil.equals(accountType, "PROD")){
            ProductInfo productInfo = new ProductInfo();
            List<Map<String, Object>>  itemInfoList = productInfoService.listMap(productInfo);
            modelMap.put("itemInfoList", itemInfoList);
        }else{
            ChannelInfoParam channelInfoParam = new ChannelInfoParam();
            List<Map<String, String>> itemInfoList = channelInfoService.selectSimpleMapInfo(channelInfoParam);
            modelMap.put("itemInfoList", itemInfoList);
        }

        return prefix + "/add";
    }

    /**
     * 新增保存子账户 绑定信息
     */
    @RequiresPermissions("account:child:view")
    @Log(title = "子账户 绑定信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AccountItemChild accountItemChild)
    {
        return toAjax(accountItemChildService.insertAccountItemChild(accountItemChild));
    }

    /**
     * 修改子账户 绑定信息
     */
    @RequiresPermissions("account:child:view")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AccountItemChild accountItemChild = accountItemChildService.selectAccountItemChildById(id);
        mmap.put("accountItemChild", accountItemChild);
        return prefix + "/edit";
    }

    /**
     * 修改保存子账户 绑定信息
     */
    @RequiresPermissions("account:child:view")
    @Log(title = "子账户 绑定信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AccountItemChild accountItemChild)
    {
        return toAjax(accountItemChildService.updateAccountItemChild(accountItemChild));
    }

    /**
     * 删除子账户 绑定信息
     */
    @RequiresPermissions("account:child:remove")
    @Log(title = "子账户 绑定信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(accountItemChildService.deleteAccountItemChildByIds(ids));
    }
}
